package com.xindaoapp.frameworkfragment;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.lidroid.xutils.ViewUtils;
import com.xindaoapp.framework.R;
import com.xindaoapp.framework.activity.NewsActivity;
import com.xindaoapp.framework.activity.TuanActivity;
import com.xindaoapp.framework.adapter.HomeGridAdapter;
import com.xindaoapp.framework.api.NewApiImpl;
import com.xindaoapp.framework.bean.Good;
import com.xindaoapp.framework.bean.SerachGood;
import com.xindaoapp.framework.bean.ShopBanner;
import com.xindaoapp.framework.bean.ShopBanner.Banner;
import com.xindaoapp.framework.utils.IRequest;
import com.xindaoapp.framework.view.MyGridView1;
import com.xindaoapp.framework.view.RollViewPager;
public class HomeFragment extends Fragment implements OnClickListener {
	private View view;
	public SlidingMenu sm;

	private ImageButton ib_title_left;
	private PullToRefreshScrollView scrollview;
	private Context context;
	private TextView description;
	private LinearLayout ll_points;
	private LinearLayout viewpager;
	private LinearLayout shop_new;
	private LinearLayout shop_tuan;
	private MyGridView1 new_grid;
	private MyGridView1 tuan_grid;
	public HomeFragment(SlidingMenu sm) {
		this.sm = sm;
	}
	public HomeFragment() {
		super();
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		context=getActivity();
		super.onActivityCreated(savedInstanceState);
	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment1, null);
		ViewUtils.inject(this, view); // 加载注释控件
		ib_title_left = (ImageButton) view.findViewById(R.id.f1_ib_title_left);
		scrollview=(PullToRefreshScrollView) view.findViewById(R.id.scrollview);
		scrollview.setMode(Mode.PULL_DOWN_TO_REFRESH);
		initUI();
		setListener();// 初始化事件监听
		return view;
	}
	@SuppressWarnings("static-access")
	private void initUI() {
		View ll=LayoutInflater.from(getActivity()).inflate(R.layout.shop_content, null);
		viewpager = (LinearLayout) ll.findViewById(R.id.shop_viewpager);
		description = (TextView) ll.findViewById(R.id.tv_image_description1);
		ll_points = (LinearLayout) ll.findViewById(R.id.ll_points);
		shop_new = (LinearLayout) ll.findViewById(R.id.shop_new);
		shop_tuan = (LinearLayout) ll.findViewById(R.id.shop_tuan);
		new_grid = (MyGridView1) ll.findViewById(R.id.new_grid);
		tuan_grid = (MyGridView1) ll.findViewById(R.id.tuan_grid);
		scrollview.getRefreshableView().addView(ll);
		scrollview.setOnRefreshListener(new OnRefreshListener<ScrollView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
				initBanner();
				initNewGoods();
				scrollview.onRefreshComplete();
			}
		});
		shop_new.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(context, NewsActivity.class);
				startActivity(intent);
			}
		});
		shop_tuan.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent=new Intent(context, TuanActivity.class);
				startActivity(intent);
			}
		});
		impl=NewApiImpl.getInstance();
		initBanner();
		initNewGoods();
		initTuan();
	}
	private HomeGridAdapter tuan;
	private void initTuan() {
		impl.getTuan(1, 3, new IRequest<String>() {
			@Override
			public void request(String t) {
				System.out.println(t);
				if(t.equals("error")){
					
				}else{
					try {
						SerachGood parse = JSON.parseObject(t, SerachGood.class);
						List<Good> info = parse.data.info;
						tuan=new HomeGridAdapter(info, context);
						tuan_grid.setAdapter(tuan);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
	}
	private HomeGridAdapter news;
	private void initNewGoods() {
		impl.getNewGoods(1, 3, new IRequest<String>() {
			@Override
			public void request(String t) {
				if(t.equals("error")){
					
				}else{
					try {
						SerachGood parse = JSON.parseObject(t, SerachGood.class);
						List<Good> info = parse.data.info;
						news=new HomeGridAdapter(info, context);
						new_grid.setAdapter(news);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
	}
	private NewApiImpl impl;
	private List<Banner> banners;
	private List<View> dotLists;
	private void initBanner() {
		impl.getBanner(new IRequest<String>() {
			@Override
			public void request(String t) {
				System.out.println(t);
				if(t.equals("error")){
					Toast.makeText(getActivity(), "数据获取失败,请重新获取", Toast.LENGTH_SHORT).show();
				}else{
					try {
						ShopBanner banner = JSON.parseObject(t, ShopBanner.class);
						if(banner.getStatus().equals("1")){
							banners = banner.getData();
							List<String> titleList = new ArrayList<String>();
							List<String> imageUrlList = new ArrayList<String>();
							for (Banner homeBanner : banners) {
								titleList.add(homeBanner.title);
								imageUrlList.add(homeBanner.getCoverpath());
							}
							prepareData(context, banners);
							
							RollViewPager rollViewPager = new RollViewPager(getActivity(), dotLists,R.drawable.point_normal, R.drawable.point_enable,  new RollViewPager.MyOnTouchCllickCallBack(){
								@Override
								public void OnTouchCllick(int position) {
									if(banners.get(position).getType().equals("1")&&banners.get(position).getEnable().equals("1")){
										//商品详情
//										Intent intent = new Intent(context, ProductDetailActivity1.class);
//										intent.putExtra("productId", banners.get(position % banners.size()).getLink());
//										startActivity(intent);
									}else if(banners.get(position).getType().equals("2")&&banners.get(position).getEnable().equals("1")){
										//品牌
//										Intent intent = new Intent(context, BrandListItem.class);
//										intent.putExtra("brandName", banners.get(position).getLink());
//										startActivity(intent);
									}else if(banners.get(position).getType().equals("3")&&banners.get(position).getEnable().equals("1")){
										//分类详情
//										Intent intent=new Intent(context,CatListItem.class);
//										intent.putExtra("catName", banners.get(position).getLink());
//										startActivity(intent);
									}else if(banners.get(position).getType().equals("4")&&banners.get(position).getEnable().equals("1")){
										//web
										Intent intent = new Intent();        
										intent.setAction("android.intent.action.VIEW");    
										Uri content_url = Uri.parse(banners.get(position).getLink());   
										intent.setData(content_url);  
										startActivity(intent);
									}else if(banners.get(position).getType().equals("5")&&banners.get(position).getEnable().equals("1")){
										//关键词搜索
//										Intent intent=new Intent(context,GoodsSerachActivity.class);
//										intent.putExtra("keyword", banners.get(position).getLink());
//										startActivity(intent);
									}
								}
							});
//							viewpager.setAdapter(adapter);
							rollViewPager.setLayoutParams(new LinearLayout.LayoutParams(
									LayoutParams.MATCH_PARENT,
									LayoutParams.WRAP_CONTENT));
							rollViewPager.setTitle(description, titleList);
							rollViewPager.setImageUrl(imageUrlList);
							rollViewPager.startRoll();
							viewpager.removeAllViews();
							viewpager.addView(rollViewPager);
						}
					} catch (Exception e) {
						
					}
				}
			}
		});
	}
	private void prepareData(Context context, List<ShopBanner.Banner> b) {
		dotLists = new ArrayList<View>();
		ll_points.removeAllViews();
		for (int i = 0; i < b.size(); i++) {
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					com.xindaoapp.framework.utils.CommonUtil.dip2px(context, 10), com.xindaoapp.framework.utils.CommonUtil.dip2px(context, 10));
			layoutParams.setMargins(5, 0, 5, 0);
			View m = new View(context);
			if (i == 0) {
				m.setBackgroundResource(R.drawable.point_enable);
			} else {
				m.setBackgroundResource(R.drawable.point_normal);
			}
			m.setLayoutParams(layoutParams);
			dotLists.add(m);
			ll_points.addView(m);
		}
	}
	@Override
	public void setMenuVisibility(boolean menuVisible) {
		super.setMenuVisibility(menuVisible);
		if (this.getView() != null)
			this.getView()
					.setVisibility(menuVisible ? View.VISIBLE : View.GONE);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.f1_ib_title_left: //打开侧边栏
			sm.toggle(); 
			break;
		default:
			break;
		}
	}
	/**
	 * 事件监听
	 */
	public void setListener() {
		ib_title_left.setOnClickListener(this);
	}
	
}

